package com.gohome.gohomechat.view.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.gohome.gohomechat.R;
import com.gohome.gohomechat.application.BaseApplication;
import com.gohome.gohomechat.db.GoHomeDBTools;
import com.gohome.gohomechat.model.entify.Contant;
import com.gohome.gohomechat.presenter.MainPresenter;
import com.gohome.gohomechat.presenter.presenterlistener.MainPresenterInterface;
import com.gohome.gohomechat.ui.adapter.MainViewPagerAdapter;
import com.gohome.gohomechat.ui.viewlistener.MainViewInterface;
import com.gohome.gohomechat.utils.ToActivityUtil;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener, MainViewInterface {
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private MainViewPagerAdapter mainViewPagerAdapter;
    private TextView main_toolbar_title;
    private ImageView main_toolbar_add;
    private MainPresenterInterface mainPresenterInterface;
    private String[] actions = {Contant.ACTION_CONTACT_CHANAGED,
            Contant.ACTION_CONTACT_REFUSED, Contant.NEW_FRIENDS_INVITATION,Contant.NEW_FRIENDS_ADD,Contant.ACCOUNT_REMOVED};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initPresenter() {
        mainPresenterInterface = new MainPresenter(this);
    }

    @Override
    protected void initView() {
        GoHomeDBTools.init(BaseApplication.getContext());
        toolbar = findView(R.id.main_toolbar);
        tabLayout = findView(R.id.main_tablayout);
        viewPager = findView(R.id.main_viewpager);
        mainViewPagerAdapter = new MainViewPagerAdapter(getSupportFragmentManager(), this);
        main_toolbar_title = findView(R.id.main_toolbar_title);
        main_toolbar_add = findView(R.id.main_toolbar_add);

        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        for (int i = 0; i < actions.length; i++) {
            IntentFilter intentFilter = new IntentFilter(actions[i]);
            localBroadcastManager.registerReceiver(broadcastReceiver, intentFilter);
        }
    }

    @Override
    protected void initData() {
        toolbar.setTitle("会话");
        setSupportActionBar(toolbar);
        viewPager.setAdapter(mainViewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < 4; i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null)
                tab.setCustomView(mainViewPagerAdapter.getTabView(i));
        }
        main_toolbar_title.setText("会话");
        viewPager.setCurrentItem(0);
        tabLayout.getTabAt(0).getCustomView().setSelected(true);
        viewPager.setOnPageChangeListener(this);

        main_toolbar_add.setOnClickListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                main_toolbar_title.setText("会话");
                main_toolbar_add.setVisibility(View.GONE);
                break;
            case 1:
                main_toolbar_title.setText("通讯录");
                main_toolbar_add.setVisibility(View.VISIBLE);
                break;
            case 2:
                main_toolbar_title.setText("其他");
                main_toolbar_add.setVisibility(View.GONE);
                break;
            case 3:
                main_toolbar_title.setText("设置");
                main_toolbar_add.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_toolbar_add:
                mainPresenterInterface.addFriend();
                break;
        }
    }

    @Override
    public void toAddFriendActivity() {
        ToActivityUtil.startToActivity(this, AddFriendActivity.class);
    }


//    public class MessageStatusBroadcastReceiver extends BroadcastReceiver {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            ((AddressFragment) FragmentFactory.createFragment(1)).address_listview_header_hint.setVisibility(View.VISIBLE);
//            tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tab_prompt).setVisibility(View.VISIBLE);
//        }
//    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Contant.ACTION_CONTACT_CHANAGED)){
                tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tab_prompt).setVisibility(View.VISIBLE);
            }
        }
    };

    @Override
    protected void onResume() {
        tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tab_prompt).setVisibility(View.GONE);
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        EMChatManager.getInstance().logout();//此方法为同步方法
        super.onDestroy();

    }
}
